package com.resou.reader.signin;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.resou.reader.R;
import com.resou.reader.data.signin.model.SignIn;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 签到结果对话框
 */
public class SignInDialog extends DialogFragment implements View.OnClickListener {
    Context mContext;
    @BindView(R.id.iv_success)
    ImageView ivSuccess;
    @BindView(R.id.tv_bonus)
    TextView tvBonus;
    @BindView(R.id.tv_bonus_info)
    TextView tvBonusInfo;
    Unbinder unbinder;
    @BindView(R.id.tv_sign_in_success)
    TextView tvSignInSuccess;
    @BindView(R.id.iv_x)
    ImageView ivX;
    @BindView(R.id.iv_bonus_book)
    ImageView ivBonusBook;
    @BindView(R.id.tv_bonus_book_name)
    TextView tvBonusBookName;
    private SignIn mSignIn;

    public static SignInDialog newInstance() {

        Bundle args = new Bundle();

        SignInDialog fragment = new SignInDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    public void setSignInfo(SignIn signIn) {
        mSignIn = signIn;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_sign_in_succss, container, false);
        unbinder = ButterKnife.bind(this, view);

        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.windowAnimations = R.style.bottomSheet_animation;
            getDialog().getWindow().setAttributes(params);
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            getDialog().setCanceledOnTouchOutside(true);
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        // 0：未获得；1：获得
        if (mSignIn != null && mSignIn.getIsShowNovel() == 1) {
            //获得了
            ivBonusBook.setVisibility(View.VISIBLE);
            tvBonusBookName.setVisibility(View.VISIBLE);
            RequestOptions options = new RequestOptions();
            options.format(DecodeFormat.PREFER_RGB_565);
            Glide.with(mContext).load(mSignIn.getSignInNovel().getCoverUrl()).apply(options).into(ivBonusBook);
            tvBonusBookName.setText(mSignIn.getSignInNovel().getNovelName());
        } else {
            ivBonusBook.setVisibility(View.GONE);
            tvBonusBookName.setVisibility(View.GONE);
        }
        if(mSignIn != null)
        {
            tvBonus.setText("恭喜您获得" + mSignIn.getSignInVoucher() + "热券");
        }
        tvBonusInfo.setText(mSignIn.getSignInPrompt());
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private static final String TAG = "SignInDialog";

    @OnClick(R.id.iv_x)
    void closeDialog() {
        dismiss();
    }

    @Override
    public void onClick(View v) {

    }
}
